package org.apache.servicecomb.samples.practise.houserush.user.center.api;

import org.apache.servicecomb.common.rest.codec.RestObjectMapperFactory;
import org.apache.servicecomb.provider.pojo.RpcReference;
import org.apache.servicecomb.provider.rest.common.RestSchema;
import org.apache.servicecomb.samples.practise.houserush.user.center.rpc.HouseOrderApi;
import org.apache.servicecomb.samples.practise.houserush.user.center.rpc.RealestateApi;
import org.apache.servicecomb.samples.practise.houserush.user.center.rpc.po.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
import java.util.List;

/**
 * @Author: Snake Man
 * @system:
 * @DateTime: 2021/12/26 22:02
 * @Description:
 */
@RestSchema(schemaId = "userCenterApiRest")
@RequestMapping("/")
public class UserCenterApiImpl  implements UserCenterApi{

    //参数一:被调用的服务名 参数二：被调用的接口id
    @RpcReference(microserviceName = "realestate", schemaId ="realestateApiRest")
    private RealestateApi realestateApi;

    @RpcReference(microserviceName = "house-order", schemaId = "houseOrderApiRest")
    private HouseOrderApi houseOrderApi;


    @GetMapping("/testPRC")
    @Override
    public String testRPC() {
        String result = "RPC";
        Date providerDate = realestateApi.testProvider();
        try {
            //"RPC : 2019-10-27 09:36:36"
            String date = RestObjectMapperFactory.getRestObjectMapper().convertToString(providerDate);
            result += ": " + date;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    //查询客户收藏列表
    @GetMapping("favorites")
    public List<Favorite> findMyFavorite(@RequestHeader int customerId){
        //查询该客户下的所有收藏
        List<Favorite> favorites = houseOrderApi.findMyFavorite(customerId);
        //遍历所有收藏
        favorites.forEach(favorite -> {
            //获取房源订单
            HouseOrder houseOrder = houseOrderApi.findOne(favorite.getHouseOrderId());
            //获取房源信息
            House house = realestateApi.findHouse(houseOrder.getHouseId());

            //设置收藏房源的信息
            //房源名称
            favorite.setHouseName(house.getName());
            //价格
            favorite.setPrice(house.getPrice());
            //建筑楼名称
            favorite.setBuilDingName(house.getBuilding().getName());
            //楼盘名称

            favorite.setRealestateName(house.getBuilding().getRealestate().getName());
            //设置房源订单状态
            favorite.setState(houseOrder.getState());
            System.out.println(favorite);
        });
        return favorites;
    }

    //查询客户收藏详情
    @GetMapping("favorites/{houseId}")
    public HouseDetail findByHouseIdDetail(@PathVariable("houseId")int houseId){
        HouseDetail houseDetail = new HouseDetail();

        House house = realestateApi.findHouse(houseId);
        houseDetail.setHouseName(house.getName());
        houseDetail.setPrice(house.getPrice());
        houseDetail.setBuilDingName(house.getBuilding().getName());

        //设置楼盘数据
        Realestate realestate = house.getBuilding().getRealestate();
        houseDetail.setRealestateName(realestate.getName());
        houseDetail.setAddress(realestate.getAddress());
        houseDetail.setType(realestate.getType());
        houseDetail.setAvgprice(realestate.getAvgprice());
        houseDetail.setUseyear(realestate.getUseyear());
        houseDetail.setUsernum(realestate.getUsernum());
        houseDetail.setArea(realestate.getArea());
        houseDetail.setBuildname(realestate.getBuildname());
        //设置房源订单数据
        HouseOrder houseOrder = houseOrderApi.findAllByHouseId(houseId);
        houseDetail.setHouseOrderId(houseOrder.getId());
        houseDetail.setState(houseOrder.getState());
        return houseDetail;
    }

    //查询房源订单
    @GetMapping("buyHouseOrderState")
    public List<HouseOrder> findMyBuyHouseNumber(@RequestHeader int customerId){
        //查询该客户下的所有房源订单
        List<HouseOrder> houseOrders = houseOrderApi.findAllByCustomerId(customerId);

        houseOrders.forEach(order -> {
            //获取房源数据
            Integer houseId = order.getHouseId();
            //查询房源数据
            House house = realestateApi.findHouse(houseId);
            order.setHouseName(house.getName());
            order.setPrice(house.getPrice());
            order.setBuilDingName(house.getBuilding().getName());

            order.setRealestateName(house.getBuilding().getRealestate().getName());
        });
        return houseOrders;
    }
}
